﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

public class LocalSetting
{
#if UNITY_STANDALONE_WIN
    public static string platform = "windows";
#elif UNITY_IPHONE
    public static string platform = "ios";
#elif UNITY_ANDROID
    public static string platform = "android";
#else
    public static string platform = "windows";
#endif
    public static string bundleUrl;
    public static string versionUrl;
    public static string libsUrl;
    public static string baseurl;
    public static string persistentDataPath = Application.persistentDataPath;
    public static string streamPath = Application.streamingAssetsPath;
    public static string assetsVersion;
    public static string firstPack;
    public static string AssetsPath;
    public static string RecordPath;

    static INIReader ini;
    static INIReader defini;
    public static AssetsOption assetsOption;
    /// <summary>
    /// 使用网络组的索引
    /// </summary>
    public static string NetScetion = "url0";
#if DEBUG
    public static bool IsDebug = true;
#else
    public static bool IsDebug = false;
#endif
#if UNITY_EDITOR
    public static bool IsEditor = true;
#else
    public static bool IsEditor = false;
#endif
    public static bool IsChannel = false;
    
    public static void Initial(string UrlSetting)
    {
        AssetsPath = persistentDataPath + "/" + platform;
        if (!Directory.Exists(AssetsPath))
            Directory.CreateDirectory(AssetsPath);
        RecordPath = persistentDataPath + "/assets";
        if (!Directory.Exists(RecordPath))
            Directory.CreateDirectory(RecordPath);
        if (ini == null)
        {
            ini = new INIReader();
            string filepath = persistentDataPath + "/" + platform + "setting.ini";
            if (File.Exists(filepath))
            {
                ini.LoadFromFile(filepath);
            }
#if UNITY_EDITOR || !UNITY_ANDROID
            filepath = streamPath + "/BaseSetting.ini";
            if (File.Exists(filepath))
            {
                defini = new INIReader();
                defini.LoadFromFile(filepath);
            }
            else
            {
                var ta = Resources.Load<TextAsset>("BaseSetting");
                if (ta != null)
                {
                    defini = new INIReader();
                    defini.LoadData(ta.bytes);
                }
            }
#else
                byte[] dat = PlatformTool.Instance.ReadFile("BaseSetting.ini");
                if(dat!=null)
                {
                    defini = new INIReader();
                    defini.LoadData(dat);
                }
                else
                {
                    var ta = Resources.Load<TextAsset>("BaseSetting");
                    if (ta != null)
                    {
                        defini = new INIReader();
                        defini.LoadData(ta.bytes);
                    }
                }
#endif
            if (defini == null)
                return;
            assetsOption = defini.ReadObject<AssetsOption>("Option");
        }
    
#if UNITY_EDITOR
        if (UrlSetting != null)
        {
            NetScetion = UrlSetting;
        }
#else
        NetScetion = GetString("Option", "net");
        IsChannel = GetInt(NetScetion, "Channel") == 1;
#endif

        baseurl = GetUrl("AssetsUrl");
        if (baseurl == null)
            return;
        bundleUrl = baseurl + "assetsbundle/";
        libsUrl = baseurl + "libs/";
        versionUrl = GetValue(defini, assetsOption.net, "Ver");
    }
    public static INIReader Setting
    {
        get
        {
            if (ini == null)
                Initial(null);
            return ini;
        }
    }
    public static void Clear()
    {
        ini = null;
    }

    public static bool Changed;
    static string GetValue(INIReader reader, string block, string key)
    {
        if (reader == null)
            return null;
        var sec = reader.FindSection(block);
        if (sec == null)
            return null;
        return sec.GetValue(key);
    }
    static string GetValue(string block, string key)
    {
        var sec = Setting.FindSection(block);
        if (sec == null)
            return null;
        return sec.GetValue(key);
    }
    public static string GetUrl(string name)
    {
        string url = NetScetion;
        string v = GetValue(Setting, url, name);
        if (v == null)
            v = GetValue(defini, url, name);
        return v;
    }
    /// <summary>
    /// 获取字符串
    /// </summary>
    /// <param name="block"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public static string GetString(string block, string name)
    {
        string v = GetValue(Setting, block, name);
        if (v == null)
            v = GetValue(defini, block, name);
        return v;
    }
    /// <summary>
    /// 获取一个整数
    /// </summary>
    /// <param name="block"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public static int GetInt(string block, string name)
    {
        string v = GetValue(Setting,block,name);
        if (v == null)
            v = GetValue(defini,block,name);
        if (v == null)
            return 0;
        int a = 0;
        int.TryParse(v, out a);
        return a;
    }
    public static void SetString(string block, string name, string value)
    {
        var sec = Setting.FindSection(block);
        if (sec == null)
        {
            sec = new INISection();
            sec.Name = block;
            Setting.AddSection(sec);
        }
        //Changed = true;
        sec.Add(name, value);
        ini.WriteToFile(persistentDataPath + "/" + platform + "setting.ini");
    }
    public static void SetInt(string block, string name, int value)
    {
        var sec = Setting.FindSection(block);
        if (sec == null)
        {
            sec = new INISection();
            sec.Name = block;
            Setting.AddSection(sec);
        }
        //Changed = true;
        sec.Add(name, value.ToString());
        ini.WriteToFile(persistentDataPath + "/" + platform + "setting.ini");
    }
    public static void Save()
    {
        if (Changed)
        {
            ini.WriteToFile(persistentDataPath + "/" + platform + "setting.ini");
            Changed = false;
        }
    }
    public static void RemoveBlock(string block)
    {
        if (ini != null)
        {
            if (ini.RemoveSection(block))
                Changed = true;
        }
    }
}
/// <summary>
/// 资源配置
/// </summary>
public class AssetsSetting
{

    public INIReader ini;
    string Name;
    string filePath;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="name">资源名</param>
    /// <param name="path">文件路径</param>
    public AssetsSetting(string name,string path)
    {
        Name = name;
        ini = new INIReader();
        if (path != null & path != "")
        {
            filePath = path;
            ini.LoadFromFile(path); 
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="name">资源名</param>
    /// <param name="data">资源数据</param>
    public AssetsSetting(string name, byte[] data)
    {
        Name = name;
        ini = new INIReader();
        if (data != null)
            ini.LoadData(data);
    }
    public  void Clear()
    {
        ini = null;
    }

    public bool Changed;
    public string GetString(string block, string name)
    {
        var sec = ini.FindSection(block);
        if (sec == null)
            return null;
        return sec.GetValue(name);
    }
    public  int GetInt(string block, string name)
    {
        var sec = ini.FindSection(block);
        if (sec == null)
            return 0;
        string v = sec.GetValue(name);
        if (v == null)
            return 0;
        int a = 0;
        int.TryParse(v, out a);
        return a;
    }
    public  void SetString(string block, string name, string value)
    {
        var sec = ini.FindSection(block);
        if (sec == null)
        {
            sec = new INISection();
            sec.Name = block;
            ini.AddSection(sec);
        }
        Changed = true;
        sec.Add(name, value);
    }
    public void SetInt(string block, string name, int value)
    {
        var sec = ini.FindSection(block);
        if (sec == null)
        {
            sec = new INISection();
            sec.Name = block;
            ini.AddSection(sec);
        }
        Changed = true;
        sec.Add(name, value.ToString());
    }
    public void Save()
    {
        if (Changed)
        {
            ini.WriteToFile(filePath);
            Changed = false;
        }
    }
    public void ForceSave()
    {
        ini.WriteToFile(filePath);
        Changed = false;
    }
    public void RemoveBlock(string block)
    {
        if (ini != null)
        {
            if (ini.RemoveSection(block))
                Changed = true;
        }
    }
}

[Serializable]
public class AssetsOption
{
    public string net;
    public bool aab;
}